<template>
    <div class="app-container">
        <el-card class="box-card">
            <el-button type="primary" size="default" @click="handleAdd">新增</el-button>
            
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="名称" prop="name"></el-table-column>
                
                <el-table-column label="图标" prop="icon">
                    <template slot-scope="{row}">
                        <el-image v-if="row.icon" :src="row.icon" fit="fill" class="image" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="选中样式" prop="selected_special">
                    <template slot-scope="{row}">
                        <el-image v-if="row.selected_special" :src="row.selected_special" fit="fill" class="image" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="静态图" prop="image">
                    <template slot-scope="{row}">
                        <el-image v-if="row.image" :src="row.image" fit="fill" class="image" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="特效" prop="special">
                    <template slot-scope="{row}">
                        <el-image v-if="row.special" :src="row.special" fit="fill" class="image" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="is_open">
                    <template slot-scope="{row}">
                        <el-tag v-if="row.is_open" type="success" size="normal"  effect="dark">开启</el-tag>
                        <el-tag v-else type="warning" size="normal"  effect="dark">关闭</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="created_at"></el-table-column>
                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleEdit(scope.row)"
                        >编辑
                        </el-button>
                        <!-- <el-button
                            size="mini"
                            type="text"
                            @click="handleDel(scope.row)"
                        >删除
                        </el-button> -->
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <el-dialog
            title="编辑"
            :visible.sync="dialogVisible"
            width="30%"
            @close="closeDialog('form')">
            
            <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="150px" size="normal">
                <el-form-item label="名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入名称"
                    />
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <div class="awardBox">
                        <div class="awardInfo" :class="{'hide_box': formData.icon, 'show_box': !formData.backcloth}">
                            <img v-if="formData.icon" :src="formData.icon" class="avatar" />
                            <el-upload
                                v-else
                                ref="upload"
                                :auto-upload="true"
                                :http-request="customizeUpload"
                                :on-success="handlePictureSuccess1"
                                action=""
                                class="upload-demo"
                                :limit="1"
                                list-type="picture-card"
                            >
                                <i slot="default" class="el-icon-plus"></i>
                            </el-upload>
                            <span class="closeIcon" @click="handlePictureRemove1"><i class="el-icon-delete"></i></span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="选中样式" prop="selected_special">
                    <div class="awardBox">

                        <div class="awardInfo" :class="{'hide_box': formData.selected_special, 'show_box': !formData.selected_special}">
                            <div v-if="formData.selected_special">
                                <img v-if="!formData.selected_special.includes('.mp4')" :src="formData.selected_special" class="avatar" />
                                <video
                                    v-else
                                    :src="formData.selected_special"
                                    autoplay
                                    controls
                                    muted
                                    style="width: 200px"
                                ></video>
                            </div>
                            <el-upload
                                v-else
                                ref="upload"
                                :auto-upload="true"
                                :http-request="customizeUpload"
                                :on-success="handlePictureSuccess2"
                                action=""
                                class="upload-demo"
                                :limit="1"
                                list-type="picture-card"
                            >
                                <i slot="default" class="el-icon-plus"></i>
                            </el-upload>
                            <span class="closeIcon" @click="handlePictureRemove2"><i class="el-icon-delete"></i></span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="静态图" prop="image">
                    <div class="awardBox">

                        <div class="awardInfo" :class="{'hide_box': formData.image, 'show_box': !formData.backcloth}">
                            <img v-if="formData.image" :src="formData.image" class="avatar" />
                            <el-upload
                                v-else
                                ref="upload"
                                :auto-upload="true"
                                :http-request="customizeUpload"
                                :on-success="handlePictureSuccess3"
                                action=""
                                class="upload-demo"
                                :limit="1"
                                list-type="picture-card"
                            >
                                <i slot="default" class="el-icon-plus"></i>
                            </el-upload>
                            <span class="closeIcon" @click="handlePictureRemove3"><i class="el-icon-delete"></i></span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="特效" prop="special">
                    <div class="awardBox">

                        <div class="awardInfo" :class="{'hide_box': formData.special, 'show_box': !formData.special}">
                            <div v-if="formData.special">
                                <img v-if="!formData.special.includes('.mp4')" :src="formData.special" class="avatar" />
                                <video
                                    v-else
                                    :src="formData.special"
                                    autoplay
                                    controls
                                    muted
                                    style="width: 200px"
                                ></video>
                            </div>
                            <el-upload
                                v-else
                                ref="upload"
                                :auto-upload="true"
                                :http-request="customizeUpload"
                                :on-success="handlePictureSuccess4"
                                action=""
                                class="upload-demo"
                                :limit="1"
                                list-type="picture-card"
                            >
                                <i slot="default" class="el-icon-plus"></i>
                            </el-upload>
                            <span class="closeIcon" @click="handlePictureRemove4"><i class="el-icon-delete"></i></span>
                        </div>
                    </div>
                </el-form-item>
                
                <el-form-item label="是否开启" prop="is_open">
                    <el-radio-group v-model="formData.is_open">
                        <el-radio :label="1">开启</el-radio>
                        <el-radio :label="0">关闭</el-radio>
                    </el-radio-group>
                </el-form-item>
                
            </el-form>
            
            <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleAllocationSkill">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>
    import { interactList, interactSave } from '@/api/petsManagement'
  	export default {
        name: 'interaction',
        data() {
            return {
                dialogTitle: 'add',
                dialogVisible: false,
                info: {},
                loading: false,
                tableData: [],
                listQuery: {
                    page: 1,
                    perPage: 10
                },
                total: 1,
                options: [
                    {
                        value: 'gift',
                        label: '礼物'
                    },
                    {
                        value: 'pet-prop',
                        label: '宠物道具'                    },
                    {
                        value: 'product',
                        label: '装扮'
                    }
                ],
                options2: [],
                formData: {
                    id: undefined,
                    name: undefined,
                    icon: undefined,
                    selected_special: undefined,
                    image: undefined,
                    special: undefined,
                    is_open: 1
                },
                rules: {
                    name: [{ required: true, message: '请输入', trigger: 'blur' }],
                    icon: [{ required: true, message: '请上传', trigger: 'blur' }],
                    // coin: [{ required: true, message: '请输入', trigger: 'blur' }],
                },
                giftName: undefined
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key] == 0 ? 0 : this.listQuery[key]
                }
                const {data, code} = await interactList(this.listQuery)
                if (code === 200) {
                    this.tableData = data.data
                    this.total = data.total
                }
                this.loading = false
        
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
                this.formData = {
                    id: undefined,
                    name: undefined,
                    icon: undefined,
                    selected_special: undefined,
                    image: undefined,
                    special: undefined,
                    is_open: 1
                }
            },
            handleEdit(row) {
                this.dialogVisible = true
                this.formData = JSON.parse(JSON.stringify(row))
            },
            handleAdd() {
                this.dialogVisible = true
            },
            handleDel(row) {
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    petPrizeDelete({
                        id: row.id,
                    }).then(res => {
                        if(res.code === 200) {
                            this.dialogVisible = false
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            })
                        }
                        this.getTableData()
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            handleAllocationSkill() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        for (let key in this.formData) {
                            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
                        }
                        interactSave(this.formData).then(res => {
                            if(res.code === 200) {
                                this.dialogVisible = false
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                                this.dialogVisible = false
                                this.getTableData()
                            }
                        })
                    }
                })
            },
            handlePictureSuccess1() {
            this.formData.icon = this.ossLastImgUrl
        },
            handlePictureRemove1() {
                this.formData.icon = undefined
            },
            handlePictureSuccess2() {
                this.formData.selected_special = this.ossLastImgUrl
            },
            handlePictureRemove2() {
                this.formData.selected_special = undefined
            },
            handlePictureSuccess3() {
                this.formData.image = this.ossLastImgUrl
            },
            handlePictureRemove3() {
                this.formData.image = undefined
            },
            handlePictureSuccess4() {
                this.formData.special = this.ossLastImgUrl
            },
            handlePictureRemove4() {
                this.formData.special = undefined
            },
        }
    }
</script>
  
<style lang="scss" scoped>
::v-deep{ 
    .hide_box{
        .el-upload--picture-card {
            display: none;
        }
    }
    .el-upload-list__item {
        margin-right: 1em !important;
    }
}

    .image {
        width: 200px;
    }
    .awardBox {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        width: 100%;
        .awardInfo {
            margin-right: 10px;
            border-radius: 4px;
            position: relative;
            overflow: hidden;
            width: 148px;
            height: 148px;
            border: 1px solid #c0ccda;
            border-radius: 6px;
            display: flex;
            align-items: center;
            justify-content: center;

            .closeIcon {
                position: absolute;
                top: 0;
                bottom: 0;
                right: 0;
                left: 0;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 10px;
                background: rgba(0,0,0,.6);
                color: #ffffff;
                overflow: hidden;
                cursor: pointer;
                font-size: 18px;
                opacity: 0;
                z-index: 0;
            }
        }
        .hide_box:hover {
            .closeIcon {
                opacity: 1;
                z-index: 1;
            }
        }
        .show_box {
            .closeIcon {
                opacity: 0;
                z-index: -1;
            }
        }
    }
</style>
  